// pages/me/login/login.js
// 获取应用实例
const app = getApp()

Page({
  data: {
    codeUrl: '',
    username: 'admin',
    password: 'admin123',
    code: '',
    uuid: '',
    token: ''
  },

  //实时获取数据
  usernameInput:function (e) {
    this.setData({
      username: e.detail.value
    })
  },
  passwordInput:function (e) {
    this.setData({
      password: e.detail.value
    })
  },
  codeInput: function (e) {
  this.setData({
    code: e.detail.value
  })
},

  getCodeAndUuidUrl() {
    var that = this;
    wx.request({
      url: 'http://localhost:8080/captchaImage',
      method:'GET',
      header:{
        'content-type': 'application/json'
      },
      success:function(res){
        console.log(res);
        that.setData({
          codeUrl: "data:image/gif;base64," + res.data.img,
          uuid: res.data.uuid
        })
      }
    })
  },

  doLogin() {
    var that = this;
    console.log(this.data.code)
    wx.request({
      url: 'http://localhost:8080/login',
      method:'POST',
      data: {
        username: that.data.username,
        password: that.data.password,
        code: that.data.code,
        uuid: that.data.uuid
      },
      header:{
        'content-type': 'application/json'
      },
      success:function(res){
        console.log(res);
        that.setData({
          token: res.data.token
        })
        app.setToken(res.data.token)
        wx.switchTab({
          url: '../../index/index',
          success (res) {
            let page = getCurrentPages().pop()
            if (page == undefined || page == null) {
              return
            }
            page.onLoad()
          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getCodeAndUuidUrl()
  }
})


